package com.realjinge.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.realjinge.entity.Danmu;
import com.realjinge.util.BaseController;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author ChenJin
 *
 */
@Controller
public class DanmuController extends BaseController {

	@RequestMapping("/getDanmu")
	@ResponseBody
	public JSONArray getDanmu(HttpServletRequest request) {
		System.out.println("getDanmuTest");
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < 100; i++) {
			String text = "TEXT_" + i;
			String color = "#FFFFFF";
			Integer size = 1;
			Integer position = 0;
			Integer time = 10 * (i + 1);
			Danmu danmu = new Danmu(text, color, size, position, time);
			jsonArray.add(danmu);
		}
		return jsonArray;
	}

	@RequestMapping("/saveDanmu")
	public String saveDanmu(HttpServletRequest request) {
		System.out.println("danmuTest");
		try {
			String danmuStr = request.getParameter("danmu");
			System.out.println(danmuStr);
			JSONObject json = JSONObject.fromObject(danmuStr);
			String text = json.getString("text");
			String color = json.getString("color");
			Integer size = Integer.parseInt(json.getString("size"));
			Integer position = Integer.parseInt(json.getString("position"));
			Integer time = Integer.parseInt(json.getString("time"));
			Danmu danmu = new Danmu(text, color, size, position, time);
			System.out.println(danmu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "test";
	}
}
